package com.fish.web.controller;

import com.fish.common.annotation.RequiresPermission;
import com.fish.common.result.Result;
import com.fish.model.dto.RoleDTO;
import com.fish.model.vo.RoleVO;
import com.fish.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色Controller
 */
@Tag(name = "角色管理", description = "角色相关接口，包括角色增删改查、权限分配等")
@Slf4j
@RestController
@RequestMapping("/api/role")
public class RoleController {
    
    @Autowired
    private RoleService roleService;
    
    /**
     * 查询所有角色
     */
    @Operation(summary = "查询所有角色", description = "查询系统中所有角色列表")
    @GetMapping("/list")
    public Result<List<RoleVO>> getAllRoles() {
        log.info("查询所有角色");
        List<RoleVO> roles = roleService.getAllRoles();
        return Result.success(roles);
    }
    
    /**
     * 根据角色ID查询角色
     */
    @Operation(summary = "根据ID查询角色", description = "根据角色ID查询角色详细信息")
    @Parameter(name = "roleId", description = "角色ID", required = true, example = "1")
    @GetMapping("/{roleId}")
    public Result<RoleVO> getRoleById(@PathVariable Long roleId) {
        log.info("查询角色，角色ID：{}", roleId);
        RoleVO role = roleService.getRoleById(roleId);
        return Result.success(role);
    }
    
    /**
     * 添加角色
     */
    @Operation(summary = "添加角色", description = "创建新的角色")
    @PostMapping("/add")
    @RequiresPermission("system:role:add")
    @com.fish.common.annotation.OperationLog("添加角色")
    public Result<RoleVO> addRole(@Validated @RequestBody RoleDTO roleDTO) {
        log.info("添加角色，角色名称：{}", roleDTO.getRoleName());
        RoleVO roleVO = roleService.addRole(roleDTO);
        return Result.success("添加成功", roleVO);
    }
    
    /**
     * 更新角色
     */
    @Operation(summary = "更新角色", description = "更新角色信息")
    @PutMapping("/update")
    @RequiresPermission("system:role:edit")
    @com.fish.common.annotation.OperationLog("更新角色")
    public Result<RoleVO> updateRole(@Validated @RequestBody RoleDTO roleDTO) {
        log.info("更新角色，角色ID：{}", roleDTO.getId());
        RoleVO roleVO = roleService.updateRole(roleDTO);
        return Result.success("更新成功", roleVO);
    }
    
    /**
     * 删除角色
     */
    @Operation(summary = "删除角色", description = "根据角色ID删除角色")
    @Parameter(name = "roleId", description = "角色ID", required = true, example = "1")
    @DeleteMapping("/delete/{roleId}")
    @RequiresPermission("system:role:delete")
    @com.fish.common.annotation.OperationLog("删除角色")
    public Result<Void> deleteRole(@PathVariable Long roleId) {
        log.info("删除角色，角色ID：{}", roleId);
        roleService.deleteRole(roleId);
        return Result.success("删除成功", null);
    }
    
    /**
     * 为角色分配菜单权限
     */
    @Operation(summary = "分配菜单权限", description = "为指定角色分配菜单权限")
    @Parameter(name = "roleId", description = "角色ID", required = true, example = "1")
    @PostMapping("/assignMenus/{roleId}")
    @RequiresPermission("system:role:edit")
    @com.fish.common.annotation.OperationLog("分配菜单权限")
    public Result<Void> assignMenusToRole(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        log.info("为角色分配菜单权限，角色ID：{}，菜单ID列表：{}", roleId, menuIds);
        roleService.assignMenusToRole(roleId, menuIds);
        return Result.success("分配权限成功", null);
    }
    
    /**
     * 为用户分配角色
     */
    @Operation(summary = "为用户分配角色", description = "为指定用户分配角色")
    @Parameter(name = "userId", description = "用户ID", required = true, example = "1")
    @PostMapping("/assign/{userId}")
    public Result<Void> assignRolesToUser(@PathVariable Long userId, @RequestBody List<Long> roleIds) {
        log.info("为用户分配角色，用户ID：{}，角色ID列表：{}", userId, roleIds);
        roleService.assignRolesToUser(userId, roleIds);
        return Result.success("分配角色成功", null);
    }
}